package com.example.a44786.myapplication;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class disi1 extends Fragment {

    //定义集合对象
    public ListView listView1;
    //定义固定列表内容
    public String[] title = new String[]{"学生", "老师"};

    //定义数据适配器
    public MyAdapter myAdapter1;

    //创建并执行视图
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
        //设置主界面对象
        View view = inflater.inflate(R.layout.disi1, viewGroup, false);
        //根据界面获取控件
        listView1 = (ListView)view.findViewById(R.id.ceshi_main_listview1);
        //将数据添加到适配器中
       myAdapter1 = new MyAdapter(title);
        //给列表添加适配器
       listView1.setAdapter(myAdapter1);
        //给列表中每一行添加点击事件
        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dialog(position);
            }
        });
        return view;

    }

    //数据适配器，数据与控件进行绑定
    public class MyAdapter extends BaseAdapter {
        //接受外界传入的数据
        private String[] titles;

        //通过构造函数，将外界的值与内部变量进行交互
        public MyAdapter(String[] title) {
            this.titles = title;
        }

        //获取传入参数长度
        @Override
        public int getCount() {
            return this.titles.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        //获取并返回数据绑定后的视图
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //声明视图对象，获取列表当中的子项内容
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.disi3, null);
            TextView textView = view.findViewById(R.id.ceshi_main_item_title);
            textView.setText(title[position]);
            return view;
        }
    }

    //通过模板窗体
    public void dialog(int position) {
        //声明一个对话框模式窗体
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        //设置弹出窗体标题
        builder.setTitle("子页面"+title[position]);
        //弹出窗体内的信息
        String str1="测试1";
        String str2="测试2";
        int int1=1;
        int int2=2;
        //设置窗体标签的详细说明
        switch (position){
            case 0:
                int1=111;
                int2=333;
                str1+="x";
                str2+="xxx";
                break;
            case 1:
                int1=222;
                int2=444;
                str1+="l";
                str2+="lll";
                break;
        }
        //获取并设置模板对象
        View view= LayoutInflater.from( getActivity() ).inflate(R.layout.dialog,null);
        //根据对象获取模板当中的控件
        final EditText editText1=(EditText)view.findViewById(R.id.dialog_edittext1);
        EditText editText2=(EditText)view.findViewById(R.id.dialog_edittext2);
        TextView textView1=(TextView)view.findViewById(R.id.dialog_textview_title1);
        TextView textView2=(TextView)view.findViewById(R.id.dialog_textview_title2);
        //给控件赋值
        textView1.setText(str1);
        textView2.setText(str2);
        editText1.setText(""+int1);
        editText2.setText(""+int2);
        //将设置控件加载到窗体中
        builder.setView(view);tyr
        //选择弹出窗体的确定所触发的时间
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String tempStr=editText1.getText().toString();
            }
        });
        //选择弹出窗体的取消按钮所触发事件
        builder.setNegativeButton("取消",null);
        //创建窗体并进行显示
        builder.create().show();

    }

}
